package com.qqtech.qquan.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.hi.model.HiHonorLevelVo;
import com.qqtech.qquan.hi.service.HiHonorLevelService;
import com.qqtech.qquan.user.dao.UserValueDao;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.model.UserValueQuery;
import com.qqtech.qquan.user.model.UserValueVo;

@Service
public class UserValueServiceImpl extends BaseServiceImpl<UserValue> implements UserValueService {
	public static final Logger log = LoggerFactory.getLogger(UserValueServiceImpl.class);

	@Resource
	private UserValueDao userValueDao;
	@Resource
	private HiHonorLevelService hiHonorLevelService;

	@Override
	protected BaseDao<UserValue> getBaseDao() {
		return userValueDao;
	}

	@Override
	public Page<UserValueVo> queryPageListByDistance(UserValue query, Pageable pageable) {
		return userValueDao.queryPageListByDistance(query, pageable);
	}

	@Override
	public void alterMoney(int userId, int alterValue) {
		userValueDao.alterMoney(userId, alterValue);
	}

	@Override
	public void alterQuanValue(int userId, int alterQuanValue) {
		UserValueVo v = userValueDao.getById(userId);
		if (v == null) {
			return;
		}
		int quanValue = v.getQuanValue() == null ? 0 : v.getQuanValue();
		int givenValueAll = quanValue + alterQuanValue;
		if (givenValueAll < 0) {
			alterQuanValue = -quanValue;
		}
		userValueDao.alterQuanValue(userId, alterQuanValue);
	}

	@Override
	public void alterHonorValue(int userId, int alterHonorValue) {
		Integer honorLevel = null;
		String honorLevelName = null;
		UserValueVo v = userValueDao.getById(userId);
		if (v == null) {
			return;
		}
		int honorValue = v.getHonorValue() == null ? 0 : v.getHonorValue().intValue();
		int honorValueAll = honorValue + alterHonorValue;
		if (honorValueAll < 0) {
			alterHonorValue = -honorValue;
			honorValueAll = 0;
		}
		List<HiHonorLevelVo> honorLevelList = hiHonorLevelService.queryAll();
		for (HiHonorLevelVo level : honorLevelList) {
			if (honorValueAll >= level.getMinimum().intValue() && honorValueAll <= level.getMaximum().intValue()) {
				honorLevel = level.getLevel();
				honorLevelName = level.getName();
				break;
			}
		}
		userValueDao.alterHonorValue(userId, alterHonorValue, honorLevel, honorLevelName);
	}

	@Override
	public void alterRegistrationId(int userId, String registrationId) {
		userValueDao.alterRegistrationId(userId, registrationId);
	}

	@Override
	public int delete(UserValue query) {
		return 0;
	}

	@Override
	public int deleteById(Integer id) {
		return 0;
	}

	@Override
	public int deleteAll() {
		return 0;
	}

	@Override
	public void deleteByIdInBatch(List<Integer> idList) {
		// nothing to do
	}

	@Override
	public int setShowLocation(Integer userId, boolean isShowLocation) throws BizzException {
		if (userId == null || userId == 0) {
			throw new BizzException("用户id为空！");
		}

		UserValue updateUserValue = new UserValue();
		updateUserValue.setId(userId);
		if (isShowLocation) {
			updateUserValue.setIsShowLocation(YesNoEnum.YES.getCode());
		} else {
			updateUserValue.setIsShowLocation(YesNoEnum.NO.getCode());
		}
		return userValueDao.updateById(updateUserValue);
	}

	@Override
	public boolean isShowLocation(Integer userId) {
		UserValue query = new UserValue();
		query.setId(userId);
		query.setIsShowLocation(YesNoEnum.YES.getCode());
		return userValueDao.queryCount(query) > 0;
	}

	@Override
	public int updateDrawCount(Integer id) {
		return userValueDao.updateDrawCount(id);
	}

	@Override
	public int updateMinusDrawCount(Integer id) {
		return userValueDao.updateMinusDrawCount(id);
	}

	@Override
	public Page<UserValueVo> queryUserInfo(UserValueQuery query, Pageable pageable) {
		return userValueDao.queryUserInfo(query, pageable);
	}

	@Override
	public Page<UserValueVo> queryPageListByTalent(UserValueQuery query, Pageable pageable) {
		return userValueDao.queryUserInfoByTalent(query,pageable);
	}

	@Override
	public UserValueVo queryByUserId(Integer userId) {
		return userValueDao.getById(userId);
	}

	@Override
	public List<UserValueVo> queryByUserIds(List<Integer> userIds) {
		UserValueQuery userValueQuery = new UserValueQuery();
		userValueQuery.setIncludeUserIds(userIds);
		return userValueDao.queryUserInfoByUserIds(userValueQuery);
	}
}